package com.wlc.thread.demo05;

import java.util.concurrent.*;

/**
 * @author 王立朝
 * @description 线程池创建线程
 * @date 2021-11-11 09:01:46
 */
public class TestThread3 {

    public static void main(String[] args) {

        /**
         * corePoolSize – 要保留在池中的线​​程数，即使它们处于空闲状态，除非设置了allowCoreThreadTimeOut
         * maximumPoolSize – 池中允许的最大线程数
         * keepAliveTime – 当线程数大于核心数时，这是多余空闲线程在终止前等待新任务的最长时间。
         * unit – keepAliveTime参数的时间单位
         * workQueue – 用于在执行任务之前保存任务的队列。 这个队列将只保存execute方法提交的Runnable任务。
         */
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 10, 60L,
                TimeUnit.MINUTES, new ArrayBlockingQueue<>(4));
        for (int i = 0; i < 10; i++) {

        threadPoolExecutor.execute(new MyThread("MyThread 当前是第 "+i));
        threadPoolExecutor.execute(new MyRunnable("MyRunnable 当前是第"+i));
        // 通过在FutureTask 传一个实例化后的 CallAble 对象来实现多线程
        FutureTask futureTask = new FutureTask<>(new MyCallAble("MyCallAble 当前是第"+i));
        threadPoolExecutor.execute(futureTask);
        }
        threadPoolExecutor.shutdown();

    }



}

class MyCallAble implements Callable{
    private String callableName;

    public MyCallAble(String callableName) {
        this.callableName = callableName;
    }

    @Override
    public Object call() throws Exception {
        System.out.println("实现CallAble接口实现多线程的方式，有返回值，可以根据返回值查看多线程是否有执行 "+this.callableName);
       return this.callableName;
    }
}
/**
 * 实现Runnable接口实现多线程
 */
class MyRunnable implements Runnable{
    private String runnableName;

    public MyRunnable(String runnableName) {
        this.runnableName = runnableName;
    }

    @Override
    public void run() {
        System.out.println("实现Runnable 接口实现多线程的方式 "+runnableName);
    }
}

/**
 * 继承Thread 类实现多线程
 */
class MyThread extends Thread{
    private String threadName;
    public MyThread(String threadName){
        this.threadName = threadName;
    }
    @Override
    public void run() {
        System.out.println(this.threadName);
        super.run();
    }
}
